<template>
  <div class="login_container">
    <div class="Login_box">
      <!-- hd -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- form -->
      <el-form class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <!-- username -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- password -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- button -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 表单验证
    login() {
      this.$refs.loginFormRef.validate(async vaild => {
        if(!vaild) return;  // 如果!valid为false结束，不向下执行
        const { data: res } = await this.$http.post('login', this.loginForm); // { data: res }解构Promise中data，重命名为res对象
        // console.log(res);
        if(res.meta.status !== 200) {
          return this.$message.error('登陆失败')
        } else {
          this.$message.success('登陆成功');
          // console.log(res);
          // 1. 将登陆成功之后的token，保存到客户端sessionStorage中
          //  1.1 项目中除了登录之外的其他API接口，必须在登陆之后才能访问
          //  1.2 token只应在当前网站打开期间生效，将token保存在sessionStorage中合适
          window.sessionStorage.setItem('token', res.data.token);
          // 2. 通过编程式导航条转到后台主页，路由地址/home
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped> /* scoped生效区间，只在组件生效 */
  .login_container{background-color: #2b4b6b; height: 100%;}
  .Login_box{width: 450px; height: 300px; background-color: white; border-radius: 3px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
    .avatar_box{width: 100px; height: 100px; border-radius: 50%; border:5px solid #ccc; padding: 10px; position: absolute; left: 50%; transform: translate(-50%, -50%); background-color: white;
      img{width: 80px; height: 80px; position: absolute; left: 50%; transform: translate(-50%, 20%);}
    }
    .login_form{position: absolute; bottom: 0; width: 100%; padding: 0 15px; box-sizing: border-box;}
    .btns{display: flex; justify-content: space-between;}
  }
</style>